using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class DataListUpdateData : System.Web.UI.Page
{

    private void LoadData()
    {
        CategoryDAO cateDAO = new CategoryDAO();
        IList<CategoryInfo> categories = cateDAO.GetCategories();
        dtlCategories.DataSource = categories;
        dtlCategories.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    protected void dtlCategories_EditCommand(object source, DataListCommandEventArgs e)
    {
        dtlCategories.EditItemIndex = e.Item.ItemIndex;
        LoadData();
    }
    protected void dtlCategories_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dtlCategories.EditItemIndex = -1;
        LoadData();
    }
    protected void dtlCategories_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        // determine the primary key of selected record to update
        int id = int.Parse(dtlCategories.DataKeys[e.Item.ItemIndex].ToString());
        // get updated data from DataList
        string name = ((TextBox)e.Item.FindControl("txtCategoryName")).Text;
        string description = ((TextBox)e.Item.FindControl("txtDescription")).Text;
        // write code to update data here
        CategoryInfo cate = new CategoryInfo(id, name, description);
        CategoryDAO cateDAO = new CategoryDAO();
        cateDAO.UpdateData(cate);
        // reload data
        dtlCategories.EditItemIndex = -1;
        LoadData();
    }
    protected void dtlCategories_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        // determine the primary key of selected record to delete
        int id = int.Parse(dtlCategories.DataKeys[e.Item.ItemIndex].ToString());
        // write code to delete data here
        // your practices
        // reload data
        dtlCategories.EditItemIndex = -1;
        LoadData();
    }
}
